<?php
 /**
 +------------------------------------------------------------------------------
 * FramkPHP 快速易用PHP框架
 +------------------------------------------------------------------------------
 * @package  Framk
 * @author   shawn fon <ucic5@163.com>
 +------------------------------------------------------------------------------
 */
 
class Mysql{
		
	private $conn;
	private $persistent;
	private $db;
	/*
	* 连接数据库
	*/
  	
	public function __construct($config){
	
		$this->persistent=$config['Persistent'];
		$connect = ( $this->persistent ) ? 'mysqli_pconnect' : 'mysqli_connect';		
		$this->conn=$connect( $config['DBhost'].':'.$config['DBport'], $config['DBuser'], $config['DBpsw'])or _error('dbConnectFail');		
		//mysqli_select_db( $config['DBname'],$this->conn) or _error('DBNameError');	
		mysqli_query($this->conn,"use {$config['DBname']}") or _error('DBNameError');

		//mysqli_query('SET NAMES '. $config['DBcharSet']);		
		mysqli_query($this->conn,"set names {$config['DBcharSet']}");					 
	} 
	
	/* 
	* 执行SQL语句
	*/		
	public function query($sql){
			
		// if( $result = mysqli_query($sql, $this->conn) ){
		// 	return $result;
		// }else{
		// 	return false;
		// }
		$res=mysqli_query($this->conn,$sql);
     if(!$res){
      echo "sql语句执行失败<br>";
      echo "错误编码是".mysqli_errno($this->conn)."<br>";
      echo "错误信息是".mysqli_error($this->conn)."<br>";
     }
     return $res;				 	 
	} 
	
	/*
	* 数组
	*/		
	public function fetch_array($result, $type=MYSQLI_ASSOC) {
		
		return mysqli_fetch_array($result,$type);
	}
	/*
	* 数据记录数
	*/
	public function num_rows($result){
	 return mysqli_num_rows($result);		
	}
	/*
	* 影响记录条数
	*/	
	public function affected_rows() {
		return mysqli_affected_rows($this->conn);
	}		
	/* 
	* 获取上一次插入的id
	*/	
	public function insert_id() {
		return mysqli_insert_id($this->conn);
	}
	/*
	* 释放结果集
	*/	
	public function free_result($result) {
		return mysqli_free_result($result);
	}
	/*
	* 析构函数关闭数据库连接
	*/
	public function __destruct(){
		if( $this->persistent==false) mysqli_close($this->conn);
	}
 /*  +------------------------------------------------------------------------------ */
}//
?>